package com.gdust.hzx.order.controller;

import com.gdust.hzx.model.dto.h5.OrderInfoDto;
import com.gdust.hzx.model.entity.order.OrderInfo;
import com.gdust.hzx.model.vo.common.Result;
import com.gdust.hzx.model.vo.common.ResultCodeEnum;
import com.gdust.hzx.model.vo.h5.TradeVo;
import com.gdust.hzx.order.service.OrderInfoService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: OrderInfoController
 * Package: com.gdust.hzx.order.controller
 * Description:
 *
 * @Author syun
 * @Create 2024/12/27 21:30
 * @Version 1.0
 */
@Tag(name = "订单管理")
@RestController
@RequestMapping(value="/api/order/orderInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoController {
    //注入订单表的service
    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 确认下单接口
     * @return
     */
    @Operation(summary = "确认下单")
    @GetMapping("auth/trade")
    public Result<TradeVo> trade(){
        //自定义方法：确认下单
       TradeVo tradeVo = orderInfoService.getTrade();
       return Result.build(tradeVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 提交订单
     * @param orderInfoDto 订单信息条件
     * @return
     */
    @Operation(summary = "提交订单")
    @PostMapping("auth/submitOrder")
    public Result<Long> submitOrder(@Parameter(name = "orderInfoDto",
            description = "请求参数实体类",
            required = true) @RequestBody OrderInfoDto orderInfoDto) {
        //提交订单
        Long orderId = orderInfoService.submitOrder(orderInfoDto);
        return Result.build(orderId,ResultCodeEnum.SUCCESS);
    }


    /**
     * 支付页模块之获取订单信息
     * @param orderId 订单id
     * @return
     */
    @Operation(summary = "获取订单信息")
    @GetMapping("auth/{orderId}")
    public Result<OrderInfo> getOrderInfo(@PathVariable Long orderId){
        //获取订单信息
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderId);
        return Result.build(orderInfo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 订单模块之立刻购买接口实现
     *
     * @param skuId 商品的 SKU ID，用于识别和获取具体的商品信息
     * @return 返回一个包含交易信息（TradeVo）的 Result 对象，表示购买操作的结果
     */
    @Operation(summary = "立即购买")
    @GetMapping("auth/buy/{skuId}")
    public Result buy(@PathVariable Long skuId) {
        //立即购买
        TradeVo tradeVo = orderInfoService.buy(skuId);
        return Result.build(tradeVo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 订单模块之获取订单分页列表
     *
     * @param page 当前页码
     * @param limit 每页显示的记录数
     * @param orderStatus 订单状态，默认为空字符串，表示不筛选订单状态
     * @return 返回包含分页数据的 Result 对象，Result 中封装了 PageInfo<OrderInfo> 分页数据
     */
    @Operation(summary = "获取订单分页列表")
    @GetMapping("auth/{page}/{limit}")
    public Result<PageInfo<OrderInfo>> list(@PathVariable Integer page,
                                            @PathVariable Integer limit,
                                            @RequestParam(required = false, defaultValue = "") Integer orderStatus) {
        //获取订单分页列表：根据当前页、每页记录数和订单状态进行分页查询。
        PageInfo<OrderInfo> pageInfo =
                orderInfoService.findOrderPage(page,limit,orderStatus);
        return Result.build(pageInfo,ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据订单编号获取订单信息（远程调用）
     *
     * @param orderNo 订单号，用于唯一标识订单
     * @return 返回与指定订单号对应的订单信息对象 OrderInfo
     */
    @Operation(summary = "获取订单信息")
    @GetMapping("auth/getOrderInfoByOrderNo/{orderNo}")
    public OrderInfo getOrderInfoByOrderNo(
            @Parameter(name = "orderId", description = "订单号", required = true)
            @PathVariable String orderNo) {
        // 调用订单服务的业务层方法，根据订单编号获取订单详细信息
        OrderInfo orderInfo = orderInfoService.getOrderInfoByOrderNo(orderNo);

        // 返回获取到的订单信息
        return orderInfo;
    }

    //更新订单状态
    @GetMapping("auth/updateOrderStatusPayed/{orderNo}")
    public Result updateOrderStatus(@PathVariable(value = "orderNo") String orderNo) {
        orderInfoService.updateOrderStatus(orderNo);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

}
